package cn.gjxblog.common.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.springframework.http.HttpStatus;

/**
 * 自定义响应对象
 * @Author guojiaxun
 * @Date 2019/9/9 17:12
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class HttpResult<T> {
    private int code;
    private String msg;
    private T data;

    public HttpResult() {

    }

    public HttpResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static HttpResult ok(){
        return new HttpResult<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),null);
    }

    public static <T> HttpResult<T> ok(T data){
        return new HttpResult<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),data);
    }

    public static <T> HttpResult<T> ok(T data,String msg){
        return new HttpResult<>(HttpStatus.OK.value(), msg, data);
    }

    public static HttpResult error(){
        return new HttpResult<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), "未知错误, 请联系管理员",null);
    }

    public static HttpResult error(String msg){
        return new HttpResult<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg,null);
    }

    public static HttpResult error(int code,String msg){
        return  new HttpResult<>(code, msg,null);
    }


}
